package com.itbaizhan.openhis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itbaizhan.openhis.domain.OperLog;
import com.itbaizhan.openhis.dto.OperLogDto;
import com.itbaizhan.openhis.service.OperLogService;
import com.itbaizhan.openhis.mapper.OperLogMapper;
import com.itbaizhan.openhis.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
* @author MR
* @description 针对表【sys_oper_log(操作日志记录)】的数据库操作Service实现
* @createDate 2025-06-05 10:57:55
*/
@Service
public class OperLogServiceImpl extends ServiceImpl<OperLogMapper, OperLog>
    implements OperLogService{
	
	@Autowired
	private OperLogMapper operLogMapper;
	/**
	 * 分页查询操作日志
	 * @return
	 */
	@Override
	public DataGridView listForPage(OperLogDto operLogDto) {
		//分页对象
		Page<OperLog> page = new Page<>(operLogDto.getPageNum(),  operLogDto.getPageSize());
		//查询条件
		QueryWrapper<OperLog> wrapper = new QueryWrapper<>();
		wrapper.like(StringUtils.isNotBlank(operLogDto.getTitle()),
				OperLog.COL_TITLE,  operLogDto.getTitle());
		wrapper.like(StringUtils.isNotBlank(operLogDto.getOperName()),
				OperLog.COL_OPER_NAME,  operLogDto.getOperName());
		wrapper.eq(StringUtils.isNotBlank(operLogDto.getStatus()),
				OperLog.COL_STATUS,  operLogDto.getStatus());
		wrapper.eq(StringUtils.isNotBlank(operLogDto.getBusinessType()),
				OperLog.COL_OPERATOR_TYPE,  operLogDto.getBusinessType());
		wrapper.ge(operLogDto.getBeginTime() != null ,
				OperLog.COL_OPER_TIME,  operLogDto.getBeginTime());
		wrapper.le(operLogDto.getEndTime() != null ,
				OperLog.COL_OPER_TIME,  operLogDto.getEndTime());
		wrapper.orderByDesc(OperLog.COL_OPER_TIME);
		operLogMapper.selectPage(page, wrapper);
		return new DataGridView(page.getTotal(), page.getRecords());
	}
	
	/**
	 * 批量删除操作日志
	 * @param infoIds
	 * @return
	 */
	@Override
	public int deleteOperLogByIds(Long[] infoIds) {
		List<Long> ids = Arrays.asList(infoIds);
		if (ids != null && ids.size() > 0){
			return operLogMapper.deleteBatchIds(ids);
		}else{
			return 0;
		}
	}
	
	/**
	 * 清空操作日志
	 * @return
	 */
	@Override
	public int clearOperLog() {
		return operLogMapper.delete(null);
	}
	
	/**
	 * 保存操作日志
	 * @param operLog
	 */
	@Override
	public void insertOperLog(OperLog operLog) {
		operLogMapper.insert(operLog);
	}
}




